export default {
    data(){
        return {
            dialogEditPermission: false,
            allPermissionList: [],  // 所有菜单和按钮[暂不缓存]
            selectedPermissionMenu: null,

            selectRolePermission: [],   // 对应角色拥有的权限
        }
    },
    created() {
        this.fetchAllPermission();
    },
    methods: {
        fetchAllPermission(){
            this.$svHttp.httpGet(this.$svHttp.Api.allPermission, {
                pageIndex: 1,
                pageSize: 500
            }).then(response=>{
                if( response ) {
                    let result = response.result;
                    let list = result.records;
                    if( Array.isArray(list) ){
                        list.forEach(item=>{
                            item._selected = false;
                            item._operation = [
                                { name: '增加', _selected: false },
                                { name: '删除', _selected: false },
                                { name: '修改', _selected: false },
                                { name: '查询', _selected: false }
                            ];
                        });
                        this.allPermissionList = list;
                    }
                }
            });
        },

        selectAllOperation(reverse){    // 全选操作
            let _operation = this.selectedPermissionMenu._operation;
            if(reverse) _operation.forEach(item=>item._selected = !item._selected);
            else _operation.forEach(item=>item._selected = true);
        },

        selectAllMenu(reverse){     // 全选菜单
            if(reverse) this.allPermissionList.forEach(item=>{
                item._selected = !item._selected;
                item._operation.forEach(op=>op._selected = !op._selected);
            });
            else this.allPermissionList.forEach(item=>{
                item._selected = true;
                item._operation.forEach(op=>op._selected = true);
            });
        },

        selectPerMenu(menu){    // 选中左侧菜单, 用来展示右侧
            this.selectedPermissionMenu = menu;
        },
        perMenuChange(menu){    // 左侧菜单选中后，全选对应操作
            menu._operation.forEach(op=>op._selected = menu._selected);
        },
        resetSelectedStatus(){  // 重置状态
            this.selectedPermissionMenu = null;
            this.allPermissionList.forEach(item=>{
                item._selected = false;
                item._operation.forEach(item=>item._selected = false);
            });
        },

        initSelectedStatus(){ // 渲染对应角色选中
            this.selectRolePermission.forEach(role=>{
                let fMenu = this.allPermissionList.find(f=>f.menuCode === role.menuCode);
                if( fMenu ) {
                    fMenu._selected = true;
                    this.selectedPermissionMenu = fMenu;
                    let rights = JSON.parse(role.rights);
                    rights.forEach((right, index)=>{
                        fMenu._operation[ index ]._selected = Boolean(right);
                    });
                }
            });
        },
        handleToPermission(row){    // 打开编辑权限弹层
            this.resetSelectedStatus();
            this.editRole = row;

            this.fullscreenLoading = true;
            this.$svHttp.httpGet(this.$svHttp.Api.allPermission, {
                roleCode: row.roleCode,
                pageIndex: 1,
                pageSize: 500
            }).then(response=>{
                this.fullscreenLoading = false;
                if( response ) {
                    let result = response.result;
                    let list = result.records;
                    this.selectRolePermission = list;
                    this.initSelectedStatus();
                    this.dialogEditPermission = true;
                }
            });
        },
        
        saveRolePermission(){
            let roleCode = this.editRole.roleCode;
            let postData = [];
            this.allPermissionList.forEach(menu=>{
                if( menu._selected === false ) return ;
                let data = {
                    menuCode: menu.menuCode,
                    roleCode,
                    roleMenuId: menu.roleMenuId,
                    parentId: menu.parentId,
                    rights: JSON.stringify(menu._operation.map(item=>Number(item._selected)))
                };
                postData.push(data);
            });

            this.fullscreenLoading = true;
            this.$svHttp.httpPost(this.$svHttp.Api.permissionUpdate, {
                roleCode,
                roleName: this.editRole.roleName,
                menuCodes: postData
            }).then(response=>{
                this.fullscreenLoading = false;
                if( response ) {
                    this.dialogEditPermission = false;
                    this.$message({ type: 'success', message: '操作成功!'});
                }
            });
        }
    },
}